package com.example.ws.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ws.mapper.ChatHistoryMapper;
import com.example.ws.model.ChatHistory;
import com.example.ws.service.IChatHistoryService;
import com.example.ws.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class ChatHistoryServiceImpl implements IChatHistoryService {
    @Autowired
   private ChatHistoryMapper chatHistoryMapper;
    @Override
    public ChatHistory add(ChatHistory chatHistory) {
        if (chatHistory ==null){
            throw new RuntimeException("聊天记录实体不能为空");
        }
        chatHistory.setCreatedAt(LocalDateTime.now());
        int insert = chatHistoryMapper.insert(chatHistory);
        if (insert > 0){
            return chatHistory;
        }
        throw new RuntimeException("插入消息历史失败");
    }

    @Override
    public PageVo<ChatHistory> listChatHistory(Integer pageNumber, int pageSize) {
        QueryWrapper<ChatHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("created_at");
        IPage<ChatHistory> pageParam = new Page<>(pageNumber,pageSize);
        IPage<ChatHistory> chatHistoryIPage = chatHistoryMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(chatHistoryIPage);
    }

}
